package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("BrandController")
public class BrandController extends CmiEmaController {

	@RequestMapping("/listBrands")
	public ModelAndView listBrands(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = brandPaginationService.updatePagination(request, pagination);
		Set<Brand> brands = brandPaginationService.getPaginationBrands(pagination);

		mav.addObject("brands", brands);
		mav.addObject("entityName", "brand");
		mav.addObject("pagination", pagination);
		mav.setViewName("brand/listBrands.jsp");
		return mav;
	}

	public String listBrands() {
		return "redirect:/listBrands";
	}

	@RequestMapping("/viewBrand")
	public ModelAndView viewBrand(@RequestParam Integer brandId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("brand", brandDAO.findBrandByPrimaryKey(brandId));
		mav.setViewName("brand/viewBrand.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteBrands")
	public ModelAndView confirmDeleteBrands(
			@RequestParam(value = "deleteBrandsIds", required = true) Set<Integer> deleteBrandsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Brand> deleteBrands = brandDAO.findBrandsByIds(deleteBrandsIds);
		Set<Brand> referencedBrands = brandService.loadReferenceBrands(deleteBrands);
		boolean hasReferenced;
		if (referencedBrands.size() == 0) {
			mav.addObject("deleteEntitys", deleteBrands);
			hasReferenced = false;
		} else {
			deleteBrands.removeAll(referencedBrands);
			mav.addObject("referencedEntitys", referencedBrands);
			mav.addObject("deleteEntitys", deleteBrands);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Brand");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listBrands");
		return mav;
	}

	@RequestMapping("/deleteBrand")
	public String deleteBrand(@RequestParam Integer brandId) {
		Brand brand = brandDAO.findBrandByPrimaryKey(brandId);
		brandService.deleteBrand(brand);
		return "forward:/listBrands";
	}

	@RequestMapping("/confirmDeleteBrand")
	public ModelAndView confirmDeleteBrand(@RequestParam Integer brandId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("brand", brandDAO.findBrandByPrimaryKey(brandId));

		mav.setViewName("brand/deleteBrand.jsp");

		return mav;
	}

	@RequestMapping("/deleteBrands")
	public String deleteBrands(@RequestParam(value = "deleteBrandsId", required = true) Set<Integer> deleteBrandsId) {
		brandService.deleteBrands(deleteBrandsId);
		return "forward:/listBrands";
	}

	@RequestMapping("/editBrand")
	public ModelAndView editBrand(@RequestParam Integer brandId) {

		ModelAndView mav = new ModelAndView();

		Brand brand = brandDAO.findBrandByPrimaryKey(brandId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("brand", brand);

		// 把所有的引用brand对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(brandService.loadBrandObjectReferencedSet(brand));

		// 将编辑页面brand添加到mav中
		mav.setViewName("brand/editBrand.jsp");
		return mav;
	}

	@RequestMapping("/newBrand")
	public ModelAndView newBrand() {
		ModelAndView mav = new ModelAndView();
		Brand brand = new Brand();

		mav.addObject("brand", brand);

		// 把所有的引用brand对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(brandService.loadBrandObjectReferencedSet(brand));

		mav.addObject("newFlag", true);
		mav.setViewName("brand/editBrand.jsp");

		return mav;
	}

	@RequestMapping("/saveBrand")
	public ModelAndView saveBrand(@ModelAttribute Brand brand) {

		ModelAndView mav = new ModelAndView();

		Brand existingBrand = CmiHibernateProxyUtil.deproxy(brandDAO.findBrandByPhysicalPrimaryKey(brand.getName()));

		// 新建记录
		if (brand.getId() == null) {
			if (brand.equals(existingBrand)) {

				mav.addAllObjects(brandService.loadBrandClassReferencedSet(brand));
				// mav = editContinue(mav, brand);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("brand/editBrand.jsp");
			} else {
				brandService.saveBrand(brand);
				mav.setViewName("forward:/listBrands");
			}
		}
		// 编辑记录
		else {
			Brand originBrand = CmiHibernateProxyUtil.deproxy(brandDAO.findBrandByPrimaryKey(brand.getId()));
			if (!brand.equals(originBrand) && brand.equals(existingBrand)) {
				mav.addAllObjects(brandService.loadBrandClassReferencedSet(brand));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("brand/editBrand.jsp");
			} else {
				brandService.saveBrand(brand);
				mav.setViewName("forward:/listBrands");
			}
		}
		return mav;
	}

	@RequestMapping("/brandController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
